//Modle :0.66"
//Driver IC:SSD1306
//Interfaces:SPI-4

#include<reg51.h>
#include<intrins.h>

sbit _CS=P3^4;
//sbit _CS1=P2^1;
sbit RES=P3^3;
sbit DC=P3^2;
sbit SCK=P3^0;//serial clock input
sbit SDA=P3^1;//serial data input

unsigned char code picture_tab[]={
//0.66",64x48*,   @64x56
0xFE,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,
0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0x02,0xFE,
0xFF,0x00,0xFC,0x83,0x01,0x83,0x7E,0x00,0x00,0x80,0x80,0x00,0xFC,0x8B,0x09,0x09,
0xF0,0x00,0x00,0xFC,0x8B,0x09,0x09,0xF0,0x00,0x00,0x07,0x00,0x07,0x00,0x00,0x7C,
0x82,0x01,0x01,0x01,0x01,0x82,0x7C,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0xFF,
0x11,0x11,0x11,0x11,0x00,0x00,0xFF,0x01,0x01,0x01,0x83,0xC2,0x7C,0x00,0x00,0xFF,
0xFF,0x80,0x80,0x81,0x81,0x81,0x80,0x80,0x80,0x81,0x81,0x80,0x80,0x81,0x81,0x81,
0x80,0x80,0x80,0x80,0x81,0x81,0x81,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x81,0x81,0x81,0x81,0x80,0x80,0x80,0x80,0x81,0x81,0x81,0x81,0x81,0x80,0x81,
0x81,0x81,0x81,0x81,0x80,0x80,0x81,0x81,0x81,0x81,0x81,0x80,0x80,0x80,0x80,0xFF,
0xFF,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x80,0x80,0x80,
0x80,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x80,0x80,0x00,0x00,
0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0xFF,
0xFF,0x00,0x00,0x40,0x7F,0x40,0x00,0x1E,0x61,0x40,0x40,0x21,0x00,0x00,0x42,0x00,
0x00,0x23,0x44,0x44,0x48,0x30,0x00,0x23,0x44,0x44,0x48,0x30,0x00,0x7F,0x40,0x40,
0x40,0x21,0x1E,0x00,0x00,0x41,0x40,0x7F,0x40,0x40,0x00,0x61,0x44,0x44,0x3B,0x00,
0x3F,0x61,0x40,0x61,0x3F,0x00,0x00,0x3E,0x45,0x44,0x44,0x38,0x00,0x00,0x00,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0xFF,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};

void Delay(unsigned int time)
        {
		  unsigned int i,j;
		  for(i=0;i<time;i++)
		     for(j=0;j<20000;j++)
			       {;}
		}
void Ins_trans(unsigned char command)
  { 
    unsigned char counter;
	  _CS=0;
	  DC=0;
    for(counter=0;counter<8;counter++)
       { 
	     SCK=0;
         SDA=(command&0x80)>>7;
         command=command<<1;
	       _nop_();
		     SCK=1;
	     //  _nop_();
		     _nop_();
		     SCK=0;
        }
	DC=1;
   _CS=1;
  }
void Data_trans(unsigned char command)
  { 
    unsigned char counter;
	  _CS=0;
	  DC=1;
    for(counter=0;counter<8;counter++)
       {
         SCK=0;
	     SDA=(command&0x80)>>7;
		 command=command<<1;   
	       _nop_();
		     SCK=1;
	      // _nop_();
		     _nop_();
		     SCK=0;
       }
	DC=1;
   _CS=1;
   }

void Column_set(unsigned char column)
          {
		     Ins_trans(0x10|(column>>4));
		     Ins_trans(0x00|(column&0x0f));	 
		  }

void Page_set(unsigned char page)
          {
		    Ins_trans(0xb0+page);
		  }
 
void Screen_clear()
       {
	     unsigned char page,column;
 		  for(page=0;page<7;page++)             //page loop
		     { 
			   Page_set(page);
			   Column_set(32);	  
			   for(column=0;column<64;column++)	//column loop
			      {
				     Data_trans(0x00);
				    }
			   }
	     }

void Screen_fill()
       {
	     unsigned char page,column;
 		  for(page=0;page<7;page++)             //page loop
		     { 
			   Page_set(page);
			   Column_set(32);	  
			   for(column=0;column<64;column++)	//column loop
			      {
				     Data_trans(0xff);
				    }
			   }
	     }

void Initial()
{
	
Ins_trans(0xae);	//--turn off oled panel

Ins_trans(0x00);	//--set low column address
Ins_trans(0x10);	//--set high column address

Ins_trans(0x40);	//--set start line address

Ins_trans(0x20);	//--Set Memory Addressing Mode
Ins_trans(0x02);

Ins_trans(0x81);	//--set contrast control register
Ins_trans(0xff);

Ins_trans(0xa1);	//--set segment re-map 127 to 0   a0:0 to seg127
Ins_trans(0xc8);	//--set com(N-1)to com0  c0:com0 to com(N-1)

Ins_trans(0xa4);	//--Resume to RAM content display

Ins_trans(0xa6);	//--set normal display

Ins_trans(0xa8);	//--set multiples ratio(1to64)
Ins_trans(0x30);	//--1/64 duty

Ins_trans(0xd3);	//--set display offset
Ins_trans(0x00);	//--not offset

Ins_trans(0xd5);	//--set display clock divide ratio/oscillator frequency
Ins_trans(0x80);	//--set divide ratio

Ins_trans(0xd9);	//--set pre-charge period
Ins_trans(0xf1);

Ins_trans(0xda);	//--set com pins hardware configuration
Ins_trans(0x12);

Ins_trans(0xdb);	//--set vcomh
Ins_trans(0x40);

Ins_trans(0x8d);	//--set chare pump enable/disable
Ins_trans(0x14);	//--set(0x10) disable

Screen_clear();
Delay(5);

Ins_trans(0xAF);;	//--turn on oled panel
}

void Picture_display(unsigned char *ptr_pic)
        {
		  unsigned char page,column;
 		  for(page=0;page<7;page++)        //page loop
		     { 
			   Page_set(page);
			   Column_set(32);	  
			   for(column=0;column<64;column++)	//column loop
			      {
				     Data_trans(*ptr_pic++);
				  }
			 }
		  Delay(5);
		}

void disA5()
{
	unsigned char i,j;
	for(j=0;j<7;j++)
	{
	   Page_set(j);
	   Column_set(32);
 		for(i=0;i<64;i++)
		{
			Data_trans(0xAA);
			Data_trans(0x55);
		}
	}
}	

void main()
     { 
		  RES=0;
		  Delay(2);
		  RES=1;
		  Initial();
		  while(1)
		{
		Screen_fill();
		Delay(5);
		disA5();
		Delay(5);
		Picture_display(picture_tab);
		}
	 }


